package com.zbf.stream2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/18  11:23
 * 描述:
 */
public class Test1811Stream {

    public static void main(String[] args) {


        MyTest myTest1=new MyTest("张三",15);
        MyTest myTest2=new MyTest("张三",12);
        MyTest myTest3=new MyTest("张三3",10);
        MyTest myTest4=new MyTest("张三4",12);
        MyTest myTest5=new MyTest("张三5",17);
        MyTest myTest6=new MyTest("张三6",17);

        List<MyTest> list=new ArrayList<>();

        list.add(myTest1);
        list.add(myTest2);
        list.add(myTest3);
        list.add(myTest4);
        list.add(myTest5);
        list.add(myTest6);

        //分组
        Map<String, List<MyTest>> collect = list.stream().collect(Collectors.groupingBy(MyTest::getName));

        //求年龄的最大值，最小值
        Optional<MyTest> max = list.stream().max((t1, t2) -> {
            if (t1.getAge() == t2.getAge()) {
                return 0;
            } else if (t1.getAge() > t2.getAge()) {
                return 1;
            } else {
                return -1;
            }
        });

        //求最小值
        Optional<MyTest> min = list.stream().min((t1, t2) -> {
            if (t1.getAge() == t2.getAge()) {
                return 0;
            } else if (t1.getAge() > t2.getAge()) {
                return 1;
            } else {
                return -1;
            }
        });
        System.out.println("===最小值=="+min.get().getAge());


        //求和
        Integer collect1 = list.stream().collect(Collectors.summingInt(map -> {
            return map.getAge();
        }));

        System.out.println(collect1);


        //求平均数
        Double collect2 = list.stream().collect(Collectors.averagingDouble(myTest -> {
            return myTest.getAge();
        }));
        System.out.println(collect2);


        //过滤的用法
        Stream<MyTest> myTestStream = list.stream().filter(myTest -> {
            if (myTest.getAge() <= 12) {
                return false;
            }
            return true;
        });
        List<MyTest> collect3 = myTestStream.collect(Collectors.toList());

        collect3.forEach(mt->{
            System.out.println("====年龄："+mt.getAge()+"----名称--》"+mt.getName());
        });


    }

}
